import { UploadFileParams } from '#/axios';
import { defineStore } from 'pinia';
import { store } from '@/store';
import { getAppEnvConfig } from '@/utils/env';
import { useI18n } from '@/hooks/web/useI18n';

import { AxiosProgressEvent } from 'axios';
import { uploadLogo, deleteLogo, getLogoBase64String } from '@/api/file';
import { setAppTitle, setAppDescription } from '@/api/system/admin/settings';

import logoImg from '@/assets/images/logo.png';

const { VITE_GLOB_APP_TITLE } = getAppEnvConfig();

interface PreferenceState {
  /** 应用LOGO */
  logoSrc?: string | null;
  /** 应用标题 */
  appTitle?: string | null;
  /** 应用描述 */
  appDescription?: string | null;
  /** 应用子描述 */
  appSubdescription?: string | null;
}
export const usePreferenceStore = defineStore({
  id: 'preference',
  state: (): PreferenceState => ({
    logoSrc: undefined,
    appTitle: undefined,
    appDescription: undefined,
    appSubdescription: undefined,
  }),
  getters: {
    /** 获取LOGO */
    getLogo(state): string {
      return state.logoSrc || logoImg;
    },
    /** 获取应用标题 */
    getAppTitle(state): string {
      return state.appTitle || VITE_GLOB_APP_TITLE;
    },
    /** 获取应用描述 */
    getAppDescription(state): string {
      const { t } = useI18n();
      return state.appDescription || t('sys.login.signInTitle');
    },
    /** 获取应用子描述 */
    getAppSubdescription(state): string {
      const { t } = useI18n();
      return state.appSubdescription || t('sys.login.signInDesc');
    },
  },
  actions: {
    /** 重置LOGO */
    async resetLogo() {
      const result = await deleteLogo();
      if (result.Type === 200) {
        this.setLogo(undefined);
      }
      return result;
    },
    /** 上传LOGO */
    async uploadLogo(
      params: UploadFileParams,
      onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
    ) {
      const result = await uploadLogo(params, onUploadProgress);
      const logo = await getLogoBase64String();
      if (logo) {
        this.setLogo(logo);
      }
      return result;
    },
    /** 设置LOGO */
    setLogo(logo?: string | null) {
      this.logoSrc = logo;
      //#region 系统网站图标处理
      let $favicon = document.querySelector('link[rel="icon"]') as HTMLLinkElement;
      if ($favicon !== null) {
        $favicon.href = this.getLogo;
      } else {
        $favicon = document.createElement('link');
        $favicon.rel = 'icon';
        $favicon.href = this.getLogo;
        document.head.appendChild($favicon);
      }
      //#endregion
    },
    /** 设置应用标题 */
    async setAppTitle(title?: string | null, sendRequest = true) {
      if (sendRequest) {
        const result = await setAppTitle(title);
        if (result.Type === 200) {
          this.appTitle = title;
        }
        return result.Type === 200;
      } else {
        this.appTitle = title;
        return true;
      }
    },
    /** 设置应用描述 */
    async setAppDescription(description?: string | null, sendRequest = true) {
      if (sendRequest) {
        const result = await setAppDescription({
          Description: description,
          Subdescription: this.appSubdescription,
        });
        if (result.Type === 200) {
          this.appDescription = description;
        }
        return result.Type === 200;
      } else {
        this.appDescription = description;
        return true;
      }
    },
    /** 设置应用子描述 */
    async setAppSubdescription(subdescription?: string | null, sendRequest = true) {
      if (sendRequest) {
        const result = await setAppDescription({
          Description: this.appDescription,
          Subdescription: subdescription,
        });
        if (result.Type === 200) {
          this.appSubdescription = subdescription;
        }
        return result.Type === 200;
      } else {
        this.appSubdescription = subdescription;
        return true;
      }
    },
  },
});

// Need to be used outside the setup
export function usePreferenceStoreWithOut() {
  return usePreferenceStore(store);
}
